﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.LayoutControl;

namespace ImageProcessor.Shared
{
    public class TabController : INotifyPropertyChanged
    {
        private ITabPanel _selectedTab;

        public TabController()
        {
            Tabs = new ObservableCollection<ITabPanel>();
        }

        public ObservableCollection<ITabPanel> Tabs { get; private set; }

        public ITabPanel SelectedTab
        {
            get { return _selectedTab; }
            set
            {
                if(_selectedTab == value)
                    return;
                _selectedTab = value;
                OnPropertyChanged("SelectedTab");
            }
        }

        public void Initialize(DXTabControl tabContainer)
        {
            var assemblies = new[]
                                 {
                                     Assembly.LoadFrom("ImageProcessor.Mish.dll"),
                                     Assembly.LoadFrom("ImageProcessor.Tim.dll"),
                                     Assembly.GetExecutingAssembly()
                                 }.ToList();

            var tabPanelTypes = assemblies.SelectMany(a => a.GetTypes())
                .Where(t => t.GetInterfaces().Any(i => i == typeof (ITabPanel))).ToList();

            tabContainer.SelectionChanged += TabContainerSelectionChanged;

            foreach (var tabPanelType in tabPanelTypes)
            {
                var tabPanel = (ITabPanel) Activator.CreateInstance(tabPanelType);

                var tabItem = new DXTabItem
                                  {
                                      Content = tabPanel,
                                      Header = tabPanel.DisplayName,                                      
                                  };

                tabContainer.Items.Add(tabItem);

                Tabs.Add(tabPanel);
            }
          
            foreach(var tab in Tabs)
                tab.Load((MainViewModel) tabContainer.DataContext);
        }

        void TabContainerSelectionChanged(object sender, TabControlSelectionChangedEventArgs e)
        {
            var tabItem = (DXTabItem) (e.NewSelectedItem);
            SelectedTab = (ITabPanel) tabItem.Content;
        }
        
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public interface ITabPanel
    {
        void Load(MainViewModel mainVM);
        string DisplayName { get; }

        Control Settings { get; }
    }    
}